
#ifndef CLOUD_SUBSCRIBER_HPP_
#define CLOUD_SUBSCRIBER_HPP_

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"
#include "sensor_msgs/msg/point_cloud2.hpp"
#include <mosquitto.h> // 确保 mosquitto 头文件可用
#include <jsoncpp/json/json.h> // 确保 jsoncpp 头文件可用
#include <string>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
#include <atomic>

class PointCloudMqttSubscriber : public rclcpp_lifecycle::LifecycleNode
{
public:
    PointCloudMqttSubscriber();
    ~PointCloudMqttSubscriber() override;

private:
    // 生命周期回调函数
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_configure(const rclcpp_lifecycle::State & previous_state);
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_activate(const rclcpp_lifecycle::State & previous_state);
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_deactivate(const rclcpp_lifecycle::State & previous_state);
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_cleanup(const rclcpp_lifecycle::State & previous_state);
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_shutdown(const rclcpp_lifecycle::State & previous_state);
    rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
    on_error(const rclcpp_lifecycle::State & previous_state);

    // 原有的私有成员函数
    void on_message(const struct mosquitto_message *msg);
    static void on_message_static(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg);
    static void on_connect_static(struct mosquitto *mosq, void *obj, int rc);
    void on_connect(int rc);
    static void on_disconnect_static(struct mosquitto *mosq, void *obj, int rc);
    void on_disconnect(int rc);
    void connect_loop();
    void resubscribe_topics();

    // 用于缓存元数据的结构
    struct MetadataCache
    {
        std_msgs::msg::Header header;
        uint32_t height;
        uint32_t width;
        std::vector<sensor_msgs::msg::PointField> fields;
        bool is_bigendian;
        uint32_t point_step;
        uint32_t row_step;
        bool is_dense;

        // 默认构造函数
        MetadataCache() : height(0), width(0), is_bigendian(false), point_step(0), row_step(0), is_dense(false) {}
    };

    // 成员变量
    // 使用 unique_ptr 管理 mosquitto 资源
    std::unique_ptr<struct mosquitto, void(*)(struct mosquitto*)> mqtt_client_;

    std::string output_ros_topic_;
    std::string mqtt_data_topic_;
    std::string mqtt_meta_topic_;
    std::string mqtt_host_;
    int mqtt_port_;

    // 使用 LifecyclePublisher
    std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<sensor_msgs::msg::PointCloud2>> pointcloud_pub_;

    // 状态标志
    bool state_is_active_;
    bool is_connected_;
    std::thread connect_thread_;
    std::atomic<bool> stop_connect_thread_;
    std::mutex connect_mutex_; // 保护连接状态访问

    // 缓存最后接收到的元数据
    MetadataCache cached_metadata_;
};

#endif // POINTCLOUD_MQTT_SUBSCRIBER_HPP_